package cn.i96688.scanclient.ui;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.alibaba.fastjson.JSONObject;

/**
 * 参数设置
 * @author xys
 *
 */
@SuppressWarnings("serial")
public class PayDialogConfig extends JDialog{
	private JButton save;
	private JSlider width;
	private JSlider height;
	private JComboBox<Integer> amountFont=new JComboBox<Integer>();//金额部分字体
	private JComboBox<Integer> keyboardFont=new JComboBox<Integer>();//键盘部分字体
	private JLabel widthTxt;
	private JLabel heightTxt;
	private SaveDataListener SaveDataListener;
	public PayDialogConfig(JFrame frame){
		super(frame, true);
		setLayout(new BorderLayout());
		setTitle("支付框设置");
		setIconImage(MainForm.icon);
		
		//1.字体设置
		SuperPane fontPane=new SuperPane();
		fontPane.setBorder(BorderFactory.createTitledBorder("字体设置"));
		fontPane.setLayout(new GridBagLayout());
		add(fontPane,BorderLayout.NORTH);
		for(int i=12;i<25;i++){
			amountFont.addItem(new Integer(i));
			keyboardFont.addItem(new Integer(i));
		}
		fontPane.add(new JLabel("金额字体"),new GBC(0,0).setInsets(4));
		fontPane.add(amountFont,new GBC(1,0).setInsets(4,4,4,20));
		fontPane.add(new JLabel("键盘字体"),new GBC(2,0).setInsets(4));
		fontPane.add(keyboardFont,new GBC(3,0).setInsets(4));
		
		
		//2.大小设置
		SuperPane sizePane=new SuperPane();
		sizePane.setBorder(BorderFactory.createTitledBorder("大小设置"));
		add(sizePane,BorderLayout.CENTER);
		sizePane.setLayout(new GridBagLayout());
		
		width=new JSlider(300,800);
		height=new JSlider(300,800);
		widthTxt=new JLabel("   ");
		heightTxt=new JLabel("   ");
		
		width.setPaintTicks(true);  
        //设置主、次刻度的间距  
		width.setMajorTickSpacing(20);  
		width.setMinorTickSpacing(5); 
		width.setValue(500);
		width.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				widthTxt.setText(width.getValue()+"");
			}});
		height.setPaintTicks(true);  
        //设置主、次刻度的间距  
		height.setMajorTickSpacing(20);  
		height.setMinorTickSpacing(5); 
		height.setValue(500);
		height.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				heightTxt.setText(height.getValue()+"");
			}});
		
		sizePane.add(new JLabel("宽度"),new GBC(0,0).setInsets(0,20,0,20));
		sizePane.add(width,new GBC(1,0).setAnchor(GBC.FIRST_LINE_START).setFill(GBC.HORIZONTAL).setInsets(2));
		sizePane.add(widthTxt,new GBC(2,0).setInsets(0,20,0,20));
		sizePane.add(new JLabel("高度"),new GBC(0,1).setInsets(0,20,0,20));
		sizePane.add(height,new GBC(1,1).setAnchor(GBC.FIRST_LINE_START).setInsets(2));
		sizePane.add(heightTxt,new GBC(2,1).setInsets(0,20,0,20));
		
		//3.按钮
		SuperPane btnPane=new SuperPane();
		add(new JLabel(" "),BorderLayout.WEST);
		add(new JLabel(" "),BorderLayout.EAST);
		btnPane.setLayout(new GridBagLayout());
		add(btnPane,BorderLayout.SOUTH);
		save=new JButton("保存");
		btnPane.add(save,new GBC(2,0).setAnchor(GBC.FIRST_LINE_START).setInsets(2));
		
		init();
	}
	public void setSaveDataListener(SaveDataListener l){
		this.SaveDataListener=l;
	}
	public void setPaySize(int w,int h,int amtSize,int keySize){
		width.setValue(w);
		widthTxt.setText(w+"");
		
		heightTxt.setText(h+"");
		height.setValue(h);
		
		amountFont.setSelectedItem(new Integer(amtSize));
		keyboardFont.setSelectedItem(new Integer(keySize));
	}
	private void init(){
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				//保存支付框数据
				if(SaveDataListener!=null){
					JSONObject json=new JSONObject();
					json.put("width", width.getValue());
					json.put("height", height.getValue());
					json.put("amountSize", amountFont.getSelectedItem());
					json.put("keyboardSize", keyboardFont.getSelectedItem());
					SaveDataListener.save(json);
				}
			}});
	}
	
	
	public static interface SaveDataListener{
		void save(JSONObject json);
	}
	
	public void showForm() {
		int width=400;
		int height=220;
		Rectangle rect = getParent().getBounds();
		int x = rect.x + (rect.width - width) / 2;
		int y = rect.y + (rect.height - height) / 2;
		this.setBounds(x, y, width, height);
		setVisible(true);
	}
}
